package com.rest.util;

import android.content.Context;

import com.burgstaller.okhttp.AuthenticationCacheInterceptor;
import com.burgstaller.okhttp.CachingAuthenticatorDecorator;
import com.burgstaller.okhttp.DispatchingAuthenticator;
import com.burgstaller.okhttp.digest.CachingAuthenticator;
import com.burgstaller.okhttp.digest.Credentials;
import com.burgstaller.okhttp.digest.DigestAuthenticator;
import com.rest.client.SSLSocketClient;

import java.io.IOException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import jz.joyoung.robot.util.LogUtil;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;


/**
 * @Description: Digest auth
 * @Author: lzp
 * @CreateDate: 2022/10/12 10:03
 * @UpdateRemark: 更新说明
 */
public class HttpDigestUtils {
    private Context context;
    private CallBackResponse callback;
    static String resposes;

    public HttpDigestUtils(Context context) {
        this.context = context;
    }

    public void setOnCallback(CallBackResponse callback) {
        this.callback = callback;
    }

    public interface CallBackResponse {
        void onSuccess(String response);

        void onFail();
    }

    public void getScreenData(String url) {

        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        final Map<String, CachingAuthenticator> authCache = new ConcurrentHashMap<>();
        //授权用户名和密码
        Credentials credentials = new Credentials("spot", "0000");
        final DigestAuthenticator digestAuthenticator = new DigestAuthenticator(credentials);
        DispatchingAuthenticator authenticator = new DispatchingAuthenticator.Builder().with("digest",
                digestAuthenticator).build();
        okhttp3.Request request = new okhttp3.Request.Builder().url(url).get()
                .header("X-User-Id", "spot")
                .header("X-Google-Ad-Id", "0000").build();
        OkHttpClient client = builder.authenticator(new CachingAuthenticatorDecorator(authenticator, authCache))
                //log 拦截器
                .addNetworkInterceptor(new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
                    @Override
                    public void log(String message) {
                        LogUtil.d("HttpDigestUtils", message);
                    }
                }).setLevel(HttpLoggingInterceptor.Level.BODY))
                .addInterceptor(new AuthenticationCacheInterceptor(authCache))
                .sslSocketFactory(SSLSocketClient.getSSLSocketFactory())
                .hostnameVerifier(SSLSocketClient.getHostnameVerifier())
                .build();
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                if (callback != null) {
                    callback.onFail();
                }
                LogUtil.e("设备IP与手机不在同一局域网");
            }

            @Override
            public void onResponse(Call call, okhttp3.Response response) throws IOException {
                resposes = response.body().string();
                if (callback != null) {
                    callback.onSuccess(resposes);
                }
            }
        });
    }
}
